/* eslint-disable no-unused-vars */
import Vue from "vue";
import Vuex from "vuex";
import io from "socket.io-client";

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        count: 0,
        socket: "",
        jssj: {},
        token: '',
        username: '',
        qjzt: false
    },
    mutations: {
        set_socket(state, payload) {
            state.socket = payload;
        },
        increment(state, n) {
            state.jssj = n.data
                // console.log(n.data)
        },
        generateMixed(state, n) {
            console.log(n);
            var str = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
            var res = "";
            for (var i = 0; i < n; i++) {
                var id = Math.ceil(Math.random() * 35);
                res += str[id];
            }
            console.log(res);
            state.token = res;
            localStorage.setItem('quanxian', state.token);
        },
        // eslint-disable-next-line no-dupe-keys
        increment1(state, n) {
            state.username = n

        }

    },
    actions: {
        async connectSocket({ commit }) {
            const socket = io.connect("/?userId=1");
            // 先保存好socket对象
            socket.on("connect", async() => {
                console.log("连接成功");
                let a = 16;
                commit('generateMixed', a);

                // 先保存好socket对象
                commit("set_socket", socket);
            });
            socket.on("sendMessage", (res) => {
                console.log(res);
                commit("increment", res);

            });
        },
    },
});